<template>
    <div>
        <h1>新增用户</h1>
     <table>
        <tbody>
           <tr>
            <td>所属项目</td>
            <td><input type="text" v-model="list.uxm"></td>
           </tr>
           <tr>
            <td>户型名称</td>
            <td><input type="text" v-model="list.uname"></td>
          
            <td>户型结构</td>
            <td>
                <select v-model="list.hid" @change="Bang">
                    <option v-for="a in date" :value="a.hid">{{a.hname}}</option>
                </select>
            </td>
           </tr>
           <tr>
            <td>建筑面积</td>
            <td><input type="text" v-model="list.ares"></td>
          
            <td>套内面积</td>
            <td><input type="text" v-model="list.tnares"></td>
           </tr>
           <tr>
            <td>户型图</td>
            <td>
                <input type="file" @change="upfile">
                <img :src="'https://localhost:7136'+list.img" style="height: 100px; width: 100px;" alt="">
            </td>
           </tr>
           <tr>
            <td>true</td>
            <td><input type="radio" value="true" checked v-model="list.isAdmin">true</td>
           </tr>
           <tr>
            <td colspan="2"><input type="button" value="保存" @click="update"></td>
           
           </tr>
        </tbody>
     </table>
    </div>
</template>

<script setup lang="ts">
import{ref,onMounted} from"vue";
import axios from "axios";
import { useRouter,useRoute} from "vue-router";
const router=useRouter();
const route=useRoute();

onMounted(()=>{
    Bang();
    fantian();
})
const fantian=()=>{
   var uid=route.query.uid;
   axios.get("https://localhost:7136/api/User/GetById",{params:{uid:uid}}).then(res=>{
      list.value=res.data;

     
   })
}








const date=ref([{
    "hid": 1,
    "hname": "国风结构"
}])

const Bang=()=>{
    axios.get("https://localhost:7136/api/Bnag/Bnagdi").then(res=>{

         date.value=res.data;
    })
}







const upfile=(a:any)=>{
    var f=a.target.files[0];
    var fs=new FormData();
    fs.append("file",f);
     
    if(f.size>1024*1024){
        alert('最多不能超过1MB');
        return;
    }
    debugger
   
        axios.post('https://localhost:7136/api/Upfile/Upfile',fs).then(res=>{
        list.value.img=res.data;
      })

}







const list=ref({
    "uid": 0,
  "uxm": "string",
  "uname": "string",
  "hid": 0,
  "ares": 0,
  "tnares": 0,
  "img": "string",
  "isAdmin": true
})
const update=()=>{
     if(list.value.uname==""){
        alert('户型名称不能为空');
        return;
     }
     if(list.value.hid==0){
       alert('户型结构不能为空');
       return;
     }
     if(list.value.ares==0){
       alert('建筑面积不能为空');
       return;
     }
     if(list.value.tnares==0){
       alert('套内面积不能为空');
       return;
     }
    if(list.value.ares<60||list.value.ares>300){
        alert('建筑面积,不得小于60,不得大于300');
        return;
    }
    
   
    axios.put("https://localhost:7136/api/User/Update",list.value).then(res=>{
            if(res.data>0){
                alert('修改成功');
                router.push({
                    path:"/",
                })
            }else{
                alert('修改失败')
            }
    })
}

</script>

<style scoped>

</style>    